package com.um.jdy.mp.core.configuration;

import cn.hutool.core.collection.ListUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.List;

@Controller
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket docket(@Value("${spring.application.name}") String name, @Value("${spring.profiles.active}") String profile){
        return new Docket(DocumentationType.OAS_30)
                .enable(!profile.equals("prod"))
                .apiInfo(new ApiInfoBuilder().title(name).build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.um.jdy.mp")
                        .or(RequestHandlerSelectors.basePackage("com.um.springboot.start")))
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.ant("/api/v*/**"))
                .build()
                .globalRequestParameters(globalRequestParameters());
    }

    public List<RequestParameter> globalRequestParameters(){

        return ListUtil.toList(
                new RequestParameterBuilder()
                        .name("token")
                        .description("Token")
                        .in(ParameterType.HEADER)
                        .build(),
                new RequestParameterBuilder()
                        .name("um-session-id")
                        .description("Session ID")
                        .in(ParameterType.HEADER)
                        .build(),
            new RequestParameterBuilder()
                    .name("com-id")
                    .description("com id")
                    .in(ParameterType.HEADER)
                    .build()
        );
    }


    /**
     * 转发页面，自定义文档访问地址
     * @return
     */
    @GetMapping("/api/doc.html")
    public String index() {
        return "forward:/doc.html";
    }

    @GetMapping("/api/swagger-resources/configuration/ui")
    public String configurationUI() {
        return "forward:/swagger-resources/configuration/ui";
    }

    @GetMapping("/api/swagger-resources")
    public String resources() {
        return "forward:/swagger-resources";
    }

    @GetMapping("/api/v3/api-docs")
    public String docs() {
        return "forward:/v3/api-docs";
    }

    /**
     * 资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/api/**").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}